243749
@@ -8263,7 +8263,8 @@
private void mergeJoinTree(QB qb) {
         continue;
       }
       JoinType prevType = null;   // save join type
-      for (int j = i - 1; j >= 0; j--) {
+      boolean continueScanning = true;
+      for (int j = i - 1; j >= 0 && continueScanning; j--) {
         QBJoinTree node = trees.get(j);
         if (node == null) {
           continue;
@@ -8279,6 +8280,7 @@
private void mergeJoinTree(QB qb) {
           if (!node.getNoOuterJoin() || !target.getNoOuterJoin()) {
             if (node.getRightAliases().length + target.getRightAliases().length + 1 > 16) {
               LOG.info(ErrorMsg.JOINNODE_OUTERJOIN_MORETHAN_16);
+              continueScanning = !runCBO;
               continue;
             }
           }
@@ -8286,6 +8288,11 @@
private void mergeJoinTree(QB qb) {
           trees.set(j, null);
           continue; // continue merging with next alias
         }
+        /*
+         * for CBO provided orderings, don't attempt to reorder joins.
+         * only convert consecutive joins into n-way joins.
+         */
+        continueScanning = !runCBO;
         if (prevType == null) {
           prevType = currType;
         }
@@ -9937,7 +9944,7 @@
public void analyzeInternal(ASTNode ast) throws SemanticException {
         //prunedPartitions = ImmutableMap.copyOf(prunedPartitions);
         getMetaData(qb);
 
-        disableJoinMerge = true;
+        disableJoinMerge = false;
         sinkOp = genPlan(qb);
         LOG.info("CBO Succeeded; optimized logical plan.");
         LOG.debug(newAST.dump());
